//Page Object
import {
    storeBindingsBehavior
} from "mobx-miniprogram-bindings";
import base from "@src/store/base";

Page({
    behaviors: [storeBindingsBehavior],
    storeBindings: [{
        store: base,
        fields: ['themeColor', 'themeTitleColor']
    }],
    data: {
        imgUrl: {}
    },
    downLoadImg() {
        if (!this.data.imgUrl.fapiaoJpgUrl) {
            console.log('图片获取失败，请稍后再试')
            return;
        }
        wx.showLoading({
            title: '加载中...',
        })
        //图片保存到本地
        wx.downloadFile({
            url: this.data.imgUrl.fapiaoJpgUrl, //图片地址
            success(downres) {
                // 保存图片到相册
                wx.saveImageToPhotosAlbum({
                    filePath: downres.tempFilePath,
                    success(res) {
                        wx.hideLoading()
                        wx.showToast({
                            title: '保存成功',
                            icon: 'success',
                            duration: 2000
                        })
                    },
                    fail(err) {
                        wx.hideLoading()
                        const arr = ['saveImageToPhotosAlbum:fail:auth denied', 'saveImageToPhotosAlbum:fail auth deny', 'saveImageToPhotosAlbum:fail auth deny']
                        if (arr.indexOf(err.errMsg) > -1) {
                            wx.showModal({
                                title: '提示',
                                content: '请授权保存到相册',
                                showCancel: false,
                                success() {
                                    wx.openSetting({
                                        success(openres) {
                                            console.log("openres", openres)
                                            if (openres.authSetting['scope.writePhotosAlbum']) {
                                                wx.showToast({
                                                    title: '获取权限成功，再次点击图片即可保存',
                                                    icon: 'none',
                                                    duration: 2000 //持续的时间
                                                })
                                            } else {
                                                wx.showToast({
                                                    title: '获取权限失败，无法保存到相册',
                                                    icon: 'none',
                                                    duration: 2000
                                                })
                                            }
                                        },
                                        fail(failerr) {
                                            console.log("failerr", failerr)
                                        }
                                    })
                                }
                            })
                        }
                    }
                })
            },
            fail() {
                wx.hideLoading()
                wx.showToast({
                    title: '下载失败请稍后再试',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    onLoad(options) {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('url', (imgUrl) => {
            this.setData({
                imgUrl
            })
        })
    }
});